package com.wing.oauth2.token;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.wing.oauth2.details.userdetails.member.MemberUserDetails;
import com.wing.oauth2.details.userdetails.user.SysUserDetails;
import com.wing.oauth2.details.userdetails.warehouse.WarehouseUserDetails;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import java.util.Map;

/**
 * 对JwtAccessTokenConverter 的 enhance进行重写，加入自定义的信息
 *
 * @author
 */
public class CustomJwtAccessTokenConverter extends JwtAccessTokenConverter {

    /**
     * 这个是token增强器，想让jwt token携带额外的信息在这里处理
     *
     * @param accessToken    访问令牌
     * @param authentication 身份验证
     * @return {@link OAuth2AccessToken}
     */
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        if (accessToken instanceof DefaultOAuth2AccessToken) {
            Object principal = authentication.getPrincipal();
            Map<String, Object> additionalInfo = CollectionUtil.newHashMap();
            if (principal instanceof SysUserDetails) {
                SysUserDetails sysUserDetails = (SysUserDetails) principal;
                additionalInfo.put("userId", sysUserDetails.getUserId());
                additionalInfo.put("username", sysUserDetails.getUsername());
                additionalInfo.put("enterpriseCode", sysUserDetails.getEnterpriseCode());
                additionalInfo.put("enterpriseId", sysUserDetails.getEnterpriseId());
                additionalInfo.put("enterpriseName", sysUserDetails.getEnterpriseName());
                additionalInfo.put("departmentId",sysUserDetails.getDepartmentId());
                additionalInfo.put("postId",sysUserDetails.getPostId());
                additionalInfo.put("company_info_id",sysUserDetails.getCompanyInfoId());
                additionalInfo.put("departmentName",sysUserDetails.getDepartmentName());
                additionalInfo.put("phone",sysUserDetails.getPhone());
                additionalInfo.put("roleName",sysUserDetails.getRoleName());
                additionalInfo.put("cabAuthCodes",sysUserDetails.getCabAuthCodes());
                additionalInfo.put("wcabAuthCodes",sysUserDetails.getWcabAuthCodes());
                additionalInfo.put("roleCode",sysUserDetails.getRoleCode());
                additionalInfo.put("roleId",sysUserDetails.getRoleId());
                additionalInfo.put("channelCode",sysUserDetails.getChannelCode());
                additionalInfo.put("costCenter", sysUserDetails.getCostCenter());
                if (StrUtil.isNotBlank(sysUserDetails.getAuthMethod())) {
                    additionalInfo.put("authMethod", sysUserDetails.getAuthMethod());
                }
                if(StrUtil.isNotBlank(sysUserDetails.getNickname())){
                    additionalInfo.put("nickname",sysUserDetails.getNickname());
                }
            } else if (principal instanceof MemberUserDetails) {
                MemberUserDetails memberUserDetails = (MemberUserDetails) principal;
                additionalInfo.put("userId", memberUserDetails.getUserId());
                additionalInfo.put("username", memberUserDetails.getUsername());
                additionalInfo.put("enterpriseId", memberUserDetails.getEnterpriseId());
                additionalInfo.put("enterpriseName", memberUserDetails.getEnterpriseName());
                additionalInfo.put("enterpriseCode", memberUserDetails.getEnterpriseCode());
                additionalInfo.put("departmentId",memberUserDetails.getDepartmentId());
                additionalInfo.put("departmentName",memberUserDetails.getDepartmentName());
                additionalInfo.put("phone",memberUserDetails.getPhone());
                additionalInfo.put("roleName",memberUserDetails.getRoleName());
                additionalInfo.put("cabAuthCodes",memberUserDetails.getCabAuthCodes());
                additionalInfo.put("wcabAuthCodes",memberUserDetails.getWcabAuthCodes());
                additionalInfo.put("roleCode",memberUserDetails.getRoleCode());
                additionalInfo.put("roleId",memberUserDetails.getRoleId());
                additionalInfo.put("channelCode",memberUserDetails.getChannelCode());
                if (StrUtil.isNotBlank(memberUserDetails.getAuthMethod())) {
                    additionalInfo.put("authMethod", memberUserDetails.getAuthMethod());
                }
            } else if (principal instanceof WarehouseUserDetails){
                WarehouseUserDetails warehouseUserDetails = (WarehouseUserDetails) principal;
                additionalInfo.put("departmentId",warehouseUserDetails.getWarehouseCustomerId());
                additionalInfo.put("userId",warehouseUserDetails.getWarehouseUserId());
            }
            ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
        }
        return super.enhance(accessToken, authentication);
    }

}
